﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BPMS.Core.DataAccess;
using BPMS.Core.Builders;

namespace BPMS.Core
{
    public interface IDbContext
    {
        #region Update

        /// <summary>
        /// 根据主键更新全部字段
        /// </summary>
        /// <typeparam name="TEntity">实体</typeparam>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        int Update<TEntity>(TEntity entity) where TEntity : EntityBase;

        /// <summary>
        /// 根据条件更新全部字段
        /// </summary>
        /// <typeparam name="TEntity">实体</typeparam>
        /// <param name="entity">实体对象</param>
        /// <param name="parameters">Where条件子句参数对象</param>
        /// <returns></returns>
        int Update<TEntity>(TEntity entity, object parameters) where TEntity : EntityBase;

        /// <summary>
        /// 根据条件更新全部字段
        /// </summary>
        /// <typeparam name="TEntity">实体</typeparam>
        /// <param name="entity">实体对象</param>
        /// <param name="whereClause">Where条件子句</param>
        /// <param name="parameters">Where条件子句参数对象</param>
        /// <returns></returns>
        int Update<TEntity>(TEntity entity, string whereClause, object parameters) where TEntity : EntityBase;

        /// <summary>
        /// 根据条件跟新部分字段
        /// </summary>
        /// <typeparam name="TEntity">实体</typeparam>
        /// <param name="setClause">SET条件子句对象</param>
        /// <param name="parameters">Where条件子句参数对象<</param>
        /// <returns></returns>
        int Update<TEntity>(object setClause, object parameters) where TEntity : EntityBase;

        /// <summary>
        /// 根据条件更新部分字段
        /// </summary>
        /// <typeparam name="TEntity">实体</typeparam>
        /// <param name="setClause">SET条件子句对象</param>
        /// <param name="whereClause">Where条件子句</param>
        /// <param name="parameters">Where条件子句参数对象</param>
        /// <returns></returns>
        int Update<TEntity>(object setClause, string whereClause, object parameters) where TEntity : EntityBase;

        #endregion

        #region Delete

        /// <summary>
        /// 根据实体主键删除记录
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        int Delete<TEntity>(TEntity entity) where TEntity : EntityBase;

        /// <summary>
        /// 根据条件删除记录
        /// </summary>
        /// <typeparam name="TEntity">实体</typeparam>
        /// <param name="parameters">Where条件子句参数对象</param>
        /// <returns></returns>
        int Delete<TEntity>(object parameters) where TEntity : EntityBase;

        /// <summary>
        /// 根据条件删除记录
        /// </summary>
        /// <typeparam name="TEntity">实体</typeparam>
        /// <param name="whereClause">Where条件子句</param>
        /// <param name="parameters">Where条件子句参数对象</param>
        /// <returns></returns>
        int Delete<TEntity>(string whereClause, object parameters) where TEntity : EntityBase;

        #endregion

        #region Insert

        /// <summary>
        /// 生成记录
        /// </summary>
        /// <typeparam name="TEntity">实体</typeparam>
        /// <typeparam name="TResult">返回类型</typeparam>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        TResult Identity<TEntity, TResult>(TEntity entity) where TEntity : EntityBase;

        /// <summary>
        /// 生成记录
        /// </summary>
        /// <typeparam name="Tentity">实体</typeparam>
        /// <param name="entity">实体对象</param>
        /// <returns>是否成功</returns>
        bool Insert<Tentity>(Tentity entity) where Tentity : EntityBase;

        #endregion

        #region Query

        /// <summary>
        /// 查询单条记录
        /// </summary>
        /// <typeparam name="TEntity">实体</typeparam>
        /// <param name="parameters">参数对象</param>
        /// <returns></returns>
        TEntity QuerySingle<TEntity>(object parameters) where TEntity : EntityBase;

        /// <summary>
        /// 查询单条记录
        /// </summary>
        /// <typeparam name="TEntity">实体</typeparam>
        /// <param name="whereClause">Where条件子句</param>
        /// <param name="parameters">参数对象</param>
        /// <returns></returns>
        TEntity QuerySingle<TEntity>(string whereClause, object parameters) where TEntity : EntityBase;

        /// <summary>
        /// 查询多条记录
        /// </summary>
        /// <typeparam name="TEntity">实体</typeparam>
        /// <param name="parameters">参数对象</param>
        /// <returns></returns>
        IEnumerable<TEntity> QueryList<TEntity>(object parameters) where TEntity : EntityBase;

        /// <summary>
        /// 查询多条记录
        /// </summary>
        /// <typeparam name="TEntity">实体</typeparam>
        /// <param name="whereClause">Where条件子句</param>
        /// <param name="parameters">参数对象</param>
        /// <returns></returns>
        IEnumerable<TEntity> QueryList<TEntity>(string whereClause, object parameters) where TEntity : EntityBase;

        /// <summary>
        /// 查询单条记录
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        dynamic QuerySingle(string sql);

        /// <summary>
        /// 查询单条记录
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters">参数对象</param>
        /// <returns></returns>
        dynamic QuerySingle(string sql, object parameters);

        /// <summary>
        /// 查询多条记录
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        IEnumerable<dynamic> QueryList(string sql);

        /// <summary>
        /// 查询多条记录
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters">参数对象</param>
        /// <returns></returns>
        IEnumerable<dynamic> QueryList(string sql, object parameters);

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="TEntity">实体</typeparam>
        /// <param name="pageData">分页数据</param>
        /// <param name="parameters">查询参数对象</param>
        /// <returns></returns>
        IEnumerable<TEntity> Page<TEntity>(PageData pageData, object parameters) where TEntity : EntityBase;

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="TEntity">实体</typeparam>
        /// <param name="pageData">分页数据</param>
        /// <param name="whereClause">Where条件子句</param>
        /// <param name="parameters">参数对象</param>
        /// <returns></returns>
        IEnumerable<TEntity> Page<TEntity>(PageData pageData, string whereClause, object parameters) where TEntity : EntityBase;

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageData">分页数据</param>
        /// <param name="sql">查询Sql</param>
        /// <returns></returns>
        IEnumerable<dynamic> Page(PageData pageData, string sql);

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageData">分页数据</param>
        /// <param name="sql">查询Sql</param>
        /// <param name="parameters">参数对象</param>
        /// <returns></returns>
        IEnumerable<dynamic> Page(PageData pageData, string sql, object parameters);

        #endregion

        #region Execute

        /// <summary>
        /// 执行SQL语句并返回受影响行数
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        int ExecuteSql(string sql);

        /// <summary>
        /// 执行SQL语句并返回受影响行数
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters">参数对象</param>
        /// <returns></returns>
        int ExecuteSql(string sql, object parameters);

        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="procedure">存储过程名称</param>
        /// <param name="parameters">输入参数</param>
        /// <param name="outParameters">输出参数</param>
        /// <returns></returns>
        int ExecuteProc(string procedure, object parameters, object outParameters);

        #endregion
    }
}
